<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Auth extends Base
{
    /**
     * 账号管理首页
     * @return mixed
     */
    public function index()
    {
        $member_list = Db::table('ad_admin')->where('del_status = 1')->paginate(10);
        return $this->fetch('index', [
            'member_list' => $member_list,
        ]);
    }

    /**
     * 账号添加页面
     * @return mixed
     */
    public function create()
    {
        //查询角色
        $role = Db::table('ad_node_admin')->select();
        return $this->fetch('create', [
            'role' => $role,
        ]);
    }

    /**
     * 账号数据保存处理
     */
    public function save()
    {
        if(request()->isPost()){
            $data = input('post.');
            //密码加密
            $pass = MD5(MD5($data['pass']));

            //根据前端的的role_id去查询数据
            $new_role_id = implode(',', $data['role_id']);//把前端获取的id拼接在一起

            $role = Db::table('ad_node_admin')->where('id IN ('.$new_role_id.')')->select();

            $new_pri_list = '';
            $old_pri_list = '';
            //循环获取用户的权限
            foreach($role as $key => $value){
                $old_pri_list = explode(',', $value['pri_list_id']);
                foreach($old_pri_list as $k1 => $v1){
                    $new_pri_list = $v1.','.$new_pri_list;
                }
            }
            //用逗号把字串拼接起来
            $new_pri_list = rtrim($new_pri_list,',');

            $user_info = [
                'user_name' => $data['user_name'],
                'user_pass' => $pass,
                'nick_name' => $data['nick_name'],
                'user_phone' => $data['phone'],
                'role_id' => $new_pri_list,
                'create_time' => time(),
            ];

            //写入数据库
            try{
                $user = Db::table('ad_admin')->insert($user_info);
            }catch(Exception $e){
                $this->error($e->getError());
            }

            if($user){
                return $this->success("添加成功!你需要重新登录 权限才能生效");
            }else{
                return $this->error("添加失败");
            }
        }
    }

    /**
     * 账号信息编辑页面
     * @return mixed
     */
    public function update()
    {
        //数据获取
        $data = input('get.');
        //查询数据
        $update_mess = Db::table('ad_admin')->where('id=' . $data['id'])->find();
        //查询角色数据
        $role_list = Db::table('ad_node_admin')->select();

        return $this->fetch('update', [
            'mess' => $update_mess,
            'roles' => $role_list,
        ]);
    }

    public function update_save()
    {
        if(request()->isPost()){
            //接收数据
            $data = input('post.');

            //进行密码处理 先根据前端传过来的id进行查询 如果没有传过密码过来 则把原密码更改进数据库 如果有 则对新密码进行加密处理
            $uid = $data['id'];
            //查询用户信息
            $advertuser_info = Db::table('ad_admin')->where('id = '.$uid)->find();
            //对密码进行处理
            if($data['pass']){
                $newPass = MD5(MD5($data['pass']));
            }else{
                $newPass = $advertuser_info['user_pass'];
            }
            //根据前端的的role_id去查询数据
            $new_role_id = implode(',', $data['role_id']);//把前端获取的id拼接在一起

            $role = Db::table('ad_node_admin')->where('id IN ('.$new_role_id.')')->select();

            $new_pri_list = '';
            $old_pri_list = '';
            //循环获取用户的权限
            foreach($role as $key => $value){
                $old_pri_list = explode(',', $value['pri_list_id']);
                foreach($old_pri_list as $k1 => $v1){
                    $new_pri_list = $v1.','.$new_pri_list;
                }
            }
            //用逗号把字串拼接起来
            $new_pri_list = rtrim($new_pri_list,',');

            //组合数据
            $update_info = [
                'user_name' => $data['user_name'],
                'user_pass' => $newPass,
                'nick_name' => $data['nick_name'],
                'user_phone' => $data['phone'],
                'role_id' => $new_pri_list,
                'update_time' => time(),
            ];
            //事务处理
            try{
                //数据更改
                $user_update = Db::table('ad_admin')->where('id = '.$data['id'])->update($update_info);
            }catch(Exception $e){
                $this->error($e->getError());
            }
            //判断是否修改
            if($user_update){
                return $this->success("修改成功，你需要重新登录权限才能起效");
            }else{
                return $this->error('修改失败');
            }


        }
    }

    /**
     * 账号删除处理
     */
    public function destory()
    {
        $id = input('post.id');

        $sta = [
            'del_status' => 2,
        ];

        //更改数据状态
        $del = Db::table('ad_admin')->where('id = ' . $id)->update($sta);

        echo $del;
    }
}